package com.baizhi.service.impl;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.dao.ArticleDao;
import com.baizhi.entity.Article;
import com.baizhi.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleDao articleDao;

    //文章的查询  分页+模糊
    @Override
    public Map listArticle(int page, int limit, String name) {
        int start=(page-1)*limit;
        List<Article> articleList = articleDao.listArticle(start, limit, name);
        int count = articleDao.countArticle(name);
        Map map=new HashMap();
        map.put("data",articleList);
        map.put("count",count);
        map.put("code",0);
        map.put("msg","");
        return map;
    }

    //发布文章
    @LogAnnotation(value = "发布文章",type = "insert")
    @Override
    public Map insertArticle(Article article) {
        int i = articleDao.insertArticle(article);
        Map map=new HashMap();
        if(i>0){
            map.put("insertArticle","添加成功！");
        }else{
            map.put("insertArticle","添加失败！");
        }
        return map;
    }

    //查询一个文章对象
    @Override
    public Article selectArtById(Integer id) {
        return articleDao.selectArtById(id);
    }

    //修改文章
    @LogAnnotation(value = "修改文章内容",type = "update")
    @Override
    public Map updateArticle(Article article) {
        int i = articleDao.updateArticle(article);
        Map map=new HashMap();
        if(i>0){
            map.put("updateArticle","修改成功！");
        }else{
            map.put("updateArticle","修改失败！");
        }
        return map;
    }

    //删除文章
    @LogAnnotation(value = "删除文章",type = "delete")
    @Override
    public Map deleteArticle(Integer id) {
        int i = articleDao.deleteArticle(id);
        Map map=new HashMap();
        if(i>0){
            map.put("deleteArticle","删除成功！");
        }else{
            map.put("deleteArticle","删除失败！");
        }
        return map;
    }
}
